package com.example.demo.controller;

import com.example.demo.model.Student;
import com.example.demo.model.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

@RestController
public class WebController {
    @RequestMapping("/testdemo1")
    public void testdemo1(HttpServletRequest req, HttpServletResponse resp) {
        req.getParameter("name");
    }

    @RequestMapping("/testdemo2")
    public Object testdemo2(String name) {
        return "name";
    }

    @RequestMapping("/testdemo3")
    public Object testdemo3(String name,Integer age) {
        return "name  : " + name + " age : " + age ;
    }

    @RequestMapping("/testdemo4")
    public Object testdemo4(Student student) {
        return student.toString();
    }

    //获取form表单的参数
    @RequestMapping("/login")
    public Object login(String username,String password) {
        return "<h1>username :  " + username + "  password :" + password + "</h1>";
    }

    //获取form表单的多个参数-->封装成对象
    @RequestMapping("/reg")
    public Object reg(User user) {
        return user.toString();
    }

    //通过ajax给后端传参
    //以JSON的形式返回给前端
    @RequestMapping("/login2")
    public HashMap<String,Object> reg(@RequestBody String name, String password) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("name",name);
        map.put("password",password);
        return map;
    }

    //后端拿到JSON对象
    @RequestMapping("/login3")
    public HashMap<String,Object> login3(@RequestBody User user) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("name",user.getName());
        map.put("password",user.getPassword());
        return map;
    }

    //上传文件
    @RequestMapping("/upload")
    public Object upload(@RequestPart("myfile")MultipartFile file
                         ,String name) throws IOException {
        //拿到前端传递的文件->接收参数
        file.transferTo(new File("E:\\demo4.png"));
        return "文件上传成功";//传递成功
    }

    //获取到单个Cookie(使用多个注解获取多个Cookie)  ,获取所有Cookie->Servlet
    //从前端发来的请求中获取名字为cookiename的 cookie 赋值给 cookieName
    @RequestMapping("/getcookie")
    public Object getCookie(@CookieValue("cookiename") String cookieName) {
        return "cookiename : " + cookieName;
    }

    //获取session


    //得到header
    @RequestMapping("/getheader")
    public Object getHeader(@RequestHeader("User-Agent") String userAgent) {
        return " User-Agent : " + userAgent;
    }

    /**
     * 后端给前端重命名参数
     * 参数重命名为time,拿t参数的内容,将参数的内容赋值给time,
     * 前端传递time参数,后端就读取不到,只能传递t
     */
    @RequestMapping("/getTime")
    public Object getTime(@RequestParam("t") String time){
        //acquired =true 必须传t, false 不存在返回null  ==>是否是必要的
        return time;
    }


    /**
     * 获取URL中的参数
     * 参数不是传到queryString里面,也不是传到body里面
     * 参数直接传到URL中
     * 在参数里面传参数没有使用?  通过目录直接传参数
     */
    @RequestMapping("/geturl/{name}/{password}")
    public Object getUrl(@PathVariable String name,
                         @PathVariable String password) {
        return "name : " + name + " password : " + password;
    }



}